package com.bw.log;

import cn.hutool.json.JSON;
import cn.hutool.json.JSONUtil;
import com.bw.domain.SysLog;
import com.bw.service.SysLogService;
import com.bw.utils.common.JWTUtils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * @author bobo棒
 * @date 2024年03月09日 10:33
 */

@Aspect
@Component
public class LogAop2 {

    @Autowired
    HttpServletRequest request;
    @Autowired
    SysLogService sysLogService;



    //通过环绕通知 拦截 被Log注解标记的方法
    @Around(value = "@annotation(com.bw.log.Log)")
    public Object log(ProceedingJoinPoint pjp) throws Throwable {
      //访问用户,ip,url,请求参数,访问时间
        String userId = JWTUtils.getUserId();
        if(null==userId){
            userId="未登录用户";
        }
        //获取访问信息
        String ip = request.getRemoteAddr();
        String uri = request.getRequestURI();
        Object[] args = pjp.getArgs();
        String argsStr = JSONUtil.toJsonStr(args);
        Date date =new Date();
        //7.拦截器中将日志保存到数据库日志表（10分）
        sysLogService.save( new SysLog(null,ip,userId,uri,argsStr,date));
       return  pjp.proceed();

    }
}
